package com.lanou.tipsy.adapter.playerLists;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.lanou.tipsy.R;
import com.lanou.tipsy.data.MyEventBus;
import com.lanou.tipsy.entity.playerList.personal.PersonalList;

import java.util.ArrayList;

import de.greenrobot.event.EventBus;

/**
 * Created by FengJun on 15/10/30.
 * 主页(MainActivity)的popupWindow弹出自己的歌曲列表---适配器
 */
public class MainListAdapter extends BaseAdapter {
    public static final String TAG = "MainListAdapter";
    private ArrayList<PersonalList> datas;
    private PersonalList songsList;
    private Context context;
    private LayoutInflater inflater;
    private int position;//当前正在播放的歌曲的position

    public ArrayList<PersonalList> getDatas() {
        return datas;
    }

    public MainListAdapter(ArrayList<PersonalList> datas, Context context, int position) {
        this.datas = datas;
        this.context = context;
        this.position = position;
        inflater = LayoutInflater.from(context);
        EventBus.getDefault().register(this);
    }

    //onEvent->获得当前正在播放的歌曲的position
    public void onEvent(MyEventBus.ListPosition index) {
        this.position = index.getIndex();
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return datas != null && datas.size() > 0 ? datas.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return datas != null && datas.size() > 0 ? datas.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_personal_list, null);
            viewHolder = new ViewHolder();
            viewHolder.singerTv = (TextView) convertView.findViewById(R.id.tv_personal_singer);
            viewHolder.songNameTv = (TextView) convertView.findViewById(R.id.tv_personal_song);
            viewHolder.positionTv = (TextView) convertView.findViewById(R.id.tv_personal_position);
            viewHolder.timeTv = (TextView) convertView.findViewById(R.id.tv_personal_time);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        songsList = (PersonalList) getItem(position);
        //Log.i("test", TAG + "我的歌曲列表,输出" + songsList.getSongTitle());
        if (position + 1 < 10) {
            viewHolder.positionTv.setText("0" + (position + 1));
        } else {
            viewHolder.positionTv.setText("" + (position + 1));
        }
        if (this.position == position) {
            viewHolder.singerTv.setTextColor(context.getResources().getColor(R.color.colorAccent));
            viewHolder.songNameTv.setTextColor(context.getResources().getColor(R.color.colorAccent));
        } else {
            viewHolder.songNameTv.setTextColor(context.getResources().getColor(R.color.color_black_tint));
            viewHolder.singerTv.setTextColor(context.getResources().getColor(R.color.color_grey));
        }
        viewHolder.singerTv.setText(songsList.getSingerName());
        viewHolder.songNameTv.setText(songsList.getSongTitle());
        Long duration = songsList.getDuration();
        if (duration != null) {
            viewHolder.timeTv.setText(String.valueOf(LocalListAdapter.formatTime(songsList.getDuration())));
        } else {
            viewHolder.timeTv.setVisibility(View.GONE);
        }
        return convertView;
    }

    private class ViewHolder {
        private TextView singerTv;
        private TextView songNameTv;
        private TextView positionTv;
        private TextView timeTv;
    }
}
